package com.fingard.dsp.bank.directbank.ceb01;

import com.fingard.FGBiz;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.directbank.cmb01.CMB01Base;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;
import com.fingard.xml.XmlTextReader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class CEB01QryRefund extends CMB01Base implements IQryRefund {
    @Override
    public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
        RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
        RetAtsRefundDetail tmpRetDetail = new RetAtsRefundDetail();
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";

        FileReader reader = null;
        BufferedReader br = null;
        try {
            String tmpFileName = FGBiz.DSPHOME_Parent + File.separator + "signnotify";
            File tmpFile = new File(tmpFileName + File.separator + p_req.payAct + ".txt");
            if (!tmpFile.exists()) {
                tmpRespCode = "0";
                tmpRespInfo = "未收到退票通知";
            } else {
                StringBuilder result = new StringBuilder();
                reader = new FileReader(tmpFile);
                br = new BufferedReader(reader);//构造一个BufferedReader类来读取文件
                String s = null;
                while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                    result.append(s);
                }
                WriteBankLogLn("退票结果内容：" + result);
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result.toString(), tmpSbMsg);
                tmpRespCode = "0";
                tmpRetDetail.askNum = tmpBkReader.getFirstText("/ATSYH/TransResp/TransParam/DetailRecord/AskNum");
                tmpRetDetail.payAct = tmpBkReader.getFirstText("/ATSYH/TransResp/TransParam/DetailRecord/PayAct");
                tmpRetDetail.recAct = tmpBkReader.getFirstText("/ATSYH/TransResp/TransParam/DetailRecord/RecAct");
                tmpRetDetail.recActName = tmpBkReader.getFirstText("/ATSYH/TransResp/TransParam/DetailRecord/RecActName");
                tmpRetDetail.amount = tmpBkReader.getFirstText("/ATSYH/TransResp/TransParam/DetailRecord/Amount");
                tmpRetDetail.refundTicketDate = tmpBkReader.getFirstText("/ATSYH/TransResp/TransParam/DetailRecord/RefundTicketDate");
                tmpRetDetail.refundTicketReason = tmpBkReader.getFirstText("/ATSYH/TransResp/TransParam/DetailRecord/RefundTicketReason");
                tmpRetDetail.submitTime = tmpBkReader.getFirstText("/ATSYH/TransResp/TransParam/DetailRecord/SubmitTime");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (br != null) {
                br.close();
            }
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeBatRespInfo(tmpRespInfo);
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}

